package ChatEntry;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ChatLoginFrame extends JFrame {
    private JTextField nicknameField;
    private JButton enterButton;
    private JButton cancelButton;

    public ChatLoginFrame() {
        setTitle("局域网聊天-进入");
        setSize(400, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // 居中显示

        // 创建面板，使用边界布局
        JPanel panel = new JPanel(new BorderLayout(10, 10));
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        panel.setBackground(new Color(240, 245, 250));

        // 顶部标签面板
        JPanel labelPanel = new JPanel();
        labelPanel.setBackground(new Color(240, 245, 250));
        JLabel nicknameLabel = new JLabel("昵称：");
        nicknameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        labelPanel.add(nicknameLabel);

        // 中间输入框面板
        JPanel fieldPanel = new JPanel();
        fieldPanel.setBackground(new Color(240, 245, 250));
        nicknameField = new JTextField(20);
        nicknameField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        nicknameField.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(180, 180, 180)),
                BorderFactory.createEmptyBorder(5, 5, 5, 5)));
        fieldPanel.add(nicknameField);

        // 底部按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 0));
        buttonPanel.setBackground(new Color(240, 245, 250));

        enterButton = new JButton("进入");
        enterButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        enterButton.setPreferredSize(new Dimension(100, 35));
        enterButton.setBackground(new Color(66, 133, 244));
        enterButton.setForeground(Color.WHITE);
        enterButton.setBorder(BorderFactory.createEmptyBorder());
        enterButton.setFocusPainted(false);

        cancelButton = new JButton("取消");
        cancelButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        cancelButton.setPreferredSize(new Dimension(100, 35));
        cancelButton.setBackground(new Color(220, 220, 220));
        cancelButton.setForeground(Color.BLACK);
        cancelButton.setBorder(BorderFactory.createEmptyBorder());
        cancelButton.setFocusPainted(false);

        buttonPanel.add(enterButton);
        buttonPanel.add(cancelButton);

        // 添加各面板到主面板
        panel.add(labelPanel, BorderLayout.NORTH);
        panel.add(fieldPanel, BorderLayout.CENTER);
        panel.add(buttonPanel, BorderLayout.SOUTH);

        add(panel);

        // 按钮事件监听
        enterButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String nickname = nicknameField.getText().trim();
                if (nickname.isEmpty()) {
                    JOptionPane.showMessageDialog(ChatLoginFrame.this, "请输入昵称！", "提示", JOptionPane.WARNING_MESSAGE);
                } else {
                    // 关闭登录窗口
                    dispose();
                    // 打开聊天窗口并传入昵称
                    SwingUtilities.invokeLater(() -> {
                        new ChatFrame(nickname).setVisible(true);
                    });
                }
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose(); // 关闭窗口
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ChatLoginFrame().setVisible(true);
            }
        });
    }
}